<?php
/**
 * ブログ記事一つを表すモデル
 *
 * @group model
 */
class Item extends DataModel
{
    protected $_schema = array(
        'post_id'       => parent::INTEGER,
        'item_id'       => parent::INTEGER,
        'history_id'    => parent::INTEGER,
        'editor_id'     => parent::INTEGER,
        'update_date'   => parent::DATETIME,
        'publish_date'  => parent::DATETIME,
        
        'name'       => parent::TEXT,
        'kana'       => parent::TEXT,
        'nickname'   => parent::TEXT,
        'note'       => parent::TEXT,
        'editor_note'=> parent::TEXT,
        
        'release_day'   => parent::INTEGER,
        'release_pre'   => parent::INTEGER,
        
        'type'       => parent::INTEGER,
    );

    function isValid(Array $error = null)
    {
        if (isset($this->_edit_hito)) {
            
            $val = $this->post_id;
            if (static::checkNumber($val, 1)) {
                $error['post_id'] = '不正な値';
            }
            
            $val = $this->hito_id;
            if (static::checkNumber($val, 1)) {
                $error['hito_id'] = '不正な値';
            }

            if (!isset($this->birth_day)) {
                $val = sprintf("%04d",@$this->_birthday['Year']) . '-'
                     . sprintf("%02d",@$this->_birthday['Month']) . '-'
                     . sprintf("%02d",@$this->_birthday['Day']);
                $this->birth_day = $val;
                if (static::checkTrueDay($val) || static::checkFuture($val)) {
                    $error['birth_day'] = '不正な値';
                }
            }

            $val = $this->name;
            if (static::checkLength($val, 32, 1)) {
                $error['name'] = '1文字以上32文字以下';
            }

            $val = $this->kana;
            if (static::checkLength($val, 32, 1) || static::checkKana($val)) {
                $error['kana'] = '1文字以上32文字以下のひらがな';
            }

            $val = $this->note;
            if (static::checkLength($val, 1024, 0)) {
                $error['note'] = '1024文字以内';
            }

            $val = $this->editor_note;
            if (static::checkLength($val, 1024, 0)) {
                $error['editor_note'] = '1024文字以内';
            }
        }
        return $error;
    }
    
}